package cn.hdustea.shopNavigation.ui.view.main.amusement

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import cn.hdustea.shopNavigation.NavigationActivity
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.adapter.AmusementItemAdapter
import cn.hdustea.shopNavigation.entity.Amusement
import cn.hdustea.shopNavigation.ui.contract.AmusementContract
import cn.hdustea.shopNavigation.ui.presenter.AmusementPresenter
import com.hdustea.base.base.BaseFragment
import com.hdustea.base.widget.CustomLoadMoreView
import com.hdustea.base.widget.SpacesItemDecoration
import kotlinx.android.synthetic.main.fragment_amusement.*
import kotlinx.android.synthetic.main.item_list_amusement.*

private const val ARG_PARAM1 = "type"

class AmusementFragment : BaseFragment<AmusementContract.Presenter>(), AmusementContract.View {
    private var type: String? = null
    private var amusementAdapter: AmusementItemAdapter? = null
    private var mCurrentPage: Int = 0

    companion object {
        fun newInstance(param1: String) =
                AmusementFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                    }
                }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            type = it.getString(ARG_PARAM1)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mPresenter?.getAmusementList(0, false)
    }

    private fun initRecyclerView() {
        amusementAdapter!!.animationEnable = true
        amusementAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()
        amusementAdapter!!.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.getAmusementList(mCurrentPage + 1, true)
        }
        amusementAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        amusementAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    override fun initView() {
        val layoutManage = LinearLayoutManager(context)
        rv_amusement_frag.layoutManager = layoutManage
        rv_amusement_frag.addItemDecoration(SpacesItemDecoration(8))
        showLoading()
    }

    override fun getLayoutRes(): Int = R.layout.fragment_amusement

    override fun getPresenter(): AmusementContract.Presenter = AmusementPresenter(this)

    // recyclerView下滑时加载店铺
    override fun setAmusementList(amusementList: MutableList<Amusement>, isRefresh: Boolean) {

        /*to_navigation.setOnClickListener {
            val intent: Intent = Intent(this.context, NavigationActivity::class.java)
            startActivity(intent)
        }*/

        if (!isRefresh) {
            amusementAdapter = AmusementItemAdapter(R.layout.item_list_amusement, this.context!!)
            initRecyclerView()
            rv_amusement_frag.adapter = amusementAdapter
            amusementAdapter!!.setList(amusementList)
        } else {
            if (amusementList.isNotEmpty()) {
                mCurrentPage++
                amusementAdapter!!.addData(amusementList)
                amusementAdapter!!.loadMoreModule.loadMoreComplete()
            } else {
                amusementAdapter!!.loadMoreModule.loadMoreEnd()
            }
        }
    }

    override fun onError() {
        amusementAdapter!!.loadMoreModule.isEnableLoadMore = true
        amusementAdapter!!.loadMoreModule.loadMoreFail()
    }
}